// pages/telService/index.js
var http = require('../../utils/http');
var api = require('../../utils/api');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        disabledBtn:false,
        searchVal: "",
        childInfo: {},
        child_id: "",
        child: [],
        user_id: "",
        showSelectPopup: false,
        radioId: "",
        docList: [],
        showPopup: false,
        showOverPopup:false,
        posname:''
    },
    /** 获取专家列表 */
    getDocListFun() {
        http.getHttp(api.getPrivateHosList, {},
            (result) => {
                if (result.code == 0) {
                    result.data.forEach(ele => {
                        ele.avatar = ele.file_id.split(',')[0]
                    })
                    this.setData({
                        docList: result.data
                    })
                }
            },
            (err) => {
                console.log(err);
            },
        )
    },
    changeSearch(e) {
        this.setData({
            searchVal: e.detail
        })
        var list = this.data.docList; //查询搜索的数组
        let key = this.data.searchVal; //搜索关键词
        if (key) {
            let isContain = item =>  item.name&&item.name.indexOf(key) != -1;
            list = list.filter(isContain);
        }else{
            this.getDocListFun()
        }
        this.setData({
            docList: list
        });
    },
    init() {
        const child = wx.getStorageSync('child')
        const childInfo = wx.getStorageSync('childInfo')
        const userInfo = wx.getStorageSync('userInfo')
        this.setData({
            child_id: childInfo.id ? childInfo.id : 0,
            childInfo: childInfo,
            child: child,
            user_id: userInfo.id,
            radioId: childInfo.id
        })
        this.getDocListFun()
    },
    selectChildFun2(){
        this.setData({
            showSelectPopup: false
        })
        setTimeout(() => {
            this.init()
        }, 500);
    },
    onCloseSelectPopup() {
        this.setData({
            showSelectPopup: false
        })
    },
    /** 切换孩子 */
    changeChildFun() {
        this.setData({
            showSelectPopup: true
        })
    },
    /** 选择孩子触发 */
    onChangeRadio(e) {
        this.setData({
            radioId: e.detail.detail,
        });
    },
    selectChildFun() {
        this.data.child.forEach(ele => {
            if (this.data.radioId == ele.id) {
                wx.setStorageSync('childInfo', ele)
                if (!ele.if_vip) {
                    this.setData({
                        showSelectPopup: false
                    })
                    wx.showModal({
                        confirmText: '我知道了',
                        content: '您还不是尊享会员，无法预约，将返回到首页',
                        showCancel: false,
                        title: '温馨提示',
                        success: (result) => {
                            wx.switchTab({
                                url: '/pages/index/index',
                            })
                        },
                        fail: (res) => {},
                        complete: (res) => {},
                    })
                    return
                }
                this.init()
                this.setData({
                    showSelectPopup: false
                })
            }
        })
    },
    /** 新建预约 */
    postResrve(id) {
        http.postHttp(api.postResrve, {
                "data": {
                    "child_id": this.data.child_id,
                    "user_id": this.data.user_id,
                    "private_hos_id": id
                },
                "type": "2"
            },
            (result) => {
                if (result.code == 0) {
                    this.setData({
                        showPopup: true,
                        disabledBtn:false
                    })
                }
            },
            (err) => {
                console.log(err);
            },
        )
    },
    /** 先检测是否可以预约 */
    handleYuyue(e) {
        let id = e.currentTarget.dataset.id
        let posname = e.currentTarget.dataset.posname
        this.setData({
            disabledBtn:true,
            posname:posname
        })
        http.postHttp(api.isIfBook, {
                rtype: "2",
                personal_id: this.data.childInfo.personal_id
            },
            (result) => {
                if (result.code == 0) {
                    if (result.data.ifok) {
                        this.postResrve(id)
                    } else {
                        console.log("不能预约");
                        this.setData({
                            showOverPopup: true,
                            disabledBtn:false
                        })
                    }
                }
            },
            (err) => {
                console.log(err);
            },
        )
    },
    onClosePopup() {
        this.setData({
            showPopup: false,
            showOverPopup: false,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.init()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})